<template>
  <el-dialog
    :title="!dataForm.id ? '新增人员' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="60%"
  >
    <div class="wrappers">
      <el-form
        :model="dataForm"
        :rules="dataRule"
        label-position="right"
        ref="dataForm"
        @keyup.enter.native="dataFormSubmit()"
        label-width="80px"
        style="width:100%"
      >
        <el-form-item label="用户姓名" prop="USERNAME">
          <el-input v-model="dataForm.USERNAME"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="PHONE">
          <el-input v-model="dataForm.PHONE"></el-input>
        </el-form-item>
        <el-form-item label="登陆账号">
          <el-input v-model="dataForm.ACCOUNT"></el-input>
        </el-form-item>
        <el-form-item label="初始密码">
          <el-input v-model="dataForm.PASSWORD"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-popover v-model="showPop" ref="menuListPopover" placement="bottom-start" trigger="click">
            <el-tree
              :data="roleszTreeNodes"
              :props="labelProps"
              ref="menuListTree"
              @node-click="searchRole"
            ></el-tree>
          </el-popover>
          <el-input
            v-model="dataForm.ROLE_NAME"
            v-popover:menuListPopover
            :readonly="true"
            placeholder="点击选择"
          ></el-input>
          <!-- <el-select v-model="dataForm.ROLE_ID" placeholder="请选择" style="width:100%">
            <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>-->
        </el-form-item>

        <el-form-item label="状态">
          <el-radio v-model="dataForm.IS_ACTIVATE" :label="0">启用</el-radio>
          <el-radio v-model="dataForm.IS_ACTIVATE" :label="1">禁用</el-radio>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils";
export default {
  data() {
    return {
      visible: false,
      imageUrl: "",
      radio: "1",
      menuList: [],
      labelProps: {
        children: "children",
        label: "name"
      },
      roleszTreeNodes:[],
      dataForm: {
        USERNAME: "",
        CLASS_TYPE: "",
        ROLE_NAME:"",
        ACCOUNT: "",
        PHONE: "",
        IS_ACTIVATE: 0,
        NAME: "",
        USERNAME: ""
      },
      dataRule: {
        USERNAME: [
          { required: true, message: "选项不能为空", trigger: "blur" }
        ],
        PHONE: [{ required: true, message: "选项不能为空", trigger: "blur" }]
      },
      addOrUpdateVisible: false,
      data: [
        {
          label: "一级 1",
          value: "001",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1"
                }
              ]
            }
          ]
        }
      ],
      showPop:false,
      roleList: [],

      multipleSelection: [],
      defaultProps: {
        children: "children",
        label: "label"
      },
      tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
    };
  },
  methods: {
    getDataList() {},
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    searchRole(data) {
      console.log(data);
      if (data.GROUP_NAME == "1") {
        this.dataForm.ROLE_NAME = data.name;
        this.dataForm.ROLE_ID = data.id;
        this.showPop=false;
      }
    },
    handleNodeClick(data) {
      console.log(data);
    },
    handleAvatarSuccess() {},
    beforeAvatarUpload() {},

    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    init(id) {
      this.dataForm.USER_ID = id || 0;
      this.$http({
        url: this.$http.adornUrl("/role/extend/role"),
        method: "post",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.roleList = data.roleszTreeNodes;
        this.roleszTreeNodes = treeDataTranslate(
          data.roleszTreeNodes,
          "id",
          "pId"
        );
        this.visible = true;
      });

      if (id) {
        this.$http({
          url: this.$http.adornUrl(
            `/user/goEditU/?USER_ID=${this.dataForm.USER_ID}`
          ),
          method: "post"
        }).then(({ data }) => {
          if (data && data.result == "1") {
            this.dataForm = Object.assign({}, this.dataForm, data.pd);
          }
        });
      } else {
        this.dataForm = {
          USERNAME: "",
          CLASS_TYPE: "",
          ACCOUNT: "",
          PHONE: "",
          IS_ACTIVATE: "",
          NAME: "",
          USERNAME: ""
        };
      }
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/user/${this.dataForm.USER_ID ? "editU" : "saveU"}`
            ),
            method: "post",
            data: this.$http.adornData(this.dataForm)
          }).then(({ data }) => {
            if (data && data.result == "1") {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.message);
            }
          });
        }
      });
    }
  }
};
</script>
<style   scoped>
.wrappers {
  display: flex;
  flex: 1;

  align-items: stretch;
  border-bottom: 1px solid #cccccc;
}
.topOpt {
  background-color: #f2f2f2;
  padding: 10px 20px;
}
.topslid {
  background-color: #1890ff;
  height: 40px;
  padding: 0 10px;
  line-height: 40px;
  color: #ffffff;
  font-size: 14px;
  cursor: pointer;
}
.sider {
  border-right: 1px solid #cccccc;
  height: 100%;
}
.topBar {
  padding: 10px;
  font-size: 18px;
  font-weight: bold;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 128px;
  height: 128px;
  line-height: 128px;
  text-align: center;
  border: 1px dashed #d6d6d6;
}
.avatar {
  width: 128px;
  height: 128px;
  display: block;
}
</style>
